<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="dateRange"
        value-format="yyyy-MM-dd"
        type="daterange"
        range-separator="~"
        start-placeholder="导入开始日期"
        end-placeholder="导入结束日期"
        :editable="false"
        class="filter-item"
        @change="changeDate"
      />
      <el-input v-model="listQuery.plateNumber" placeholder="车牌号" class="filter-item" clearable style="width: 200px;" />
      <!--      <el-date-picker
        v-model="dateRange2"
        value-format="yyyy-MM-dd"
        type="daterange"
        range-separator="~"
        start-placeholder="被拒绝开始日期"
        end-placeholder="被拒绝结束日期"
        :editable="false"
        class="filter-item"
        @change="changeDate2"
      />
      <el-select v-model="listQuery.status" placeholder="结果" clearable class="filter-item" style="width: 120px;">
        <el-option v-for="item in statusOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select> -->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button v-if="type == 0" v-waves :loading="uploadLoading" class="filter-item" type="primary" icon="el-icon-upload" @click="handleUpload">导入</el-button>
      <el-button v-if="type == 0" v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="车牌号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.plateNumber }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="被拒绝时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.rejectTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="被拒绝原因" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.refuse }}</span>
        </template>
      </el-table-column>
      <el-table-column label="遗漏订单开始时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.orderBeginTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="遗漏订单结束时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.orderEndTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单里程(公里)" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.orderMileage }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="结果" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.status | statusFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="导入人" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createUser }}</span>
        </template>
      </el-table-column>
      <el-table-column label="导入时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <div v-if="dialogEditFormVisible">
      <el-dialog title="选择文件" :visible.sync="dialogEditFormVisible" :append-to-body="true" :close-on-click-modal="false" :destroy-on-close="true">
        <UploadExcel @uploadSuccess="uploadSuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getWhitelistInput } from '@/api/whitelistinput';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import UploadExcel from '@/components/Upload/excel.vue'
import { parseTime } from '@/utils';

var that;

export default {
  name: 'WhitelistInput',
  components: { Pagination, UploadExcel },
  directives: { waves },
  filters: {
    statusFilter(item) {
      const statusKeyValue = that.statusOptions.reduce((acc, cur) => {
        acc[cur.key] = cur.label;
        return acc;
      }, {});
      return statusKeyValue[item];
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      uploadLoading: false,
      downloadLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        plateNumber: '',
        status: null,
        rejectBeginTime: null,
        rejectEndTime: null,
        beginTime: '',
        endTime: ''
      },
      dateRange: [],
      dateRange2: [],
      statusOptions: [{ label: '已导入', key: 1 }, { label: '日期不符', key: 2 }, { label: '订单不符', key: 3 }, { label: '订单里程不符', key: 4 }, { label: '重复已删除', key: 0 }],
      dialogEditFormVisible: false,
      type: null
    };
  },
  created() {
    that = this;
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}'));
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}'));
    this.listQuery.beginTime = this.dateRange[0] + ' 00:00:00';
    this.listQuery.endTime = this.dateRange[1] + ' 23:59:59';
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getWhitelistInput(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    changeDate(val) {
      if (val) {
        this.listQuery.beginTime = val[0] + ' 00:00:00';
        this.listQuery.endTime = val[1] + ' 23:59:59';
      } else {
        this.listQuery.beginTime = null;
        this.listQuery.endTime = null;
      }
    },
    changeDate2(val) {
      if (val) {
        this.listQuery.rejectBeginTime = val[0] + ' 00:00:00';
        this.listQuery.rejectEndTime = val[1] + ' 23:59:59';
      } else {
        this.listQuery.rejectBeginTime = null;
        this.listQuery.rejectEndTime = null;
      }
    },
    async handleUpload() {
      this.dialogEditFormVisible = true;
    },
    uploadSuccess() {
      this.getList();
      this.dialogEditFormVisible = false;
      this.uploadLoading = false;
    },
    uploadError() {
      this.dialogEditFormVisible = false;
      this.uploadLoading = false;
    },
    async handleDownload() {
      if (this.total === 0) {
        this.$message.error('暂无数据');
        return;
      }
      this.downloadLoading = true;
      this.listQuery.pageSize = this.total;
      await getWhitelistInput(this.listQuery).then(response => {
        this.listQuery.pageSize = 10;
        if (response.data.total === 0) {
          this.$message.error('暂无数据，请重新刷新页面');
          return;
        }
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['车牌号', '结果', '导入人', '导入时间'];
          const filterVal = ['plateNumber', 'status', 'createUser', 'createTime'];
          const data = this.formatJson(response.data.list, filterVal);
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '白名单导入记录'
          });
          this.downloadLoading = false;
          this.$message({
            message: '白名单导入记录导出成功',
            type: 'success'
          });
        });
      });
    },
    formatJson(list, filterVal) {
      const statusFilter = this.$options.filters['statusFilter'];
      return list.map(v =>
        filterVal.map(j => {
          if (j === 'rejectTime' || j === 'orderBeginTime' || j === 'orderEndTime' || j === 'createTime') {
            return parseTime(v[j], '{y}-{m}-{d} {h}:{i}:{s}');
          } else if (j === 'status') {
            return statusFilter(v[j]);
          } else {
            return v[j];
          }
        })
      );
    }
  }
};
</script>
